/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.price;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.hutech09.jsale.bsn.IProductManager;
import org.hutech09.jsale.bsn.exception.ItemNotFoundException;
import org.hutech09.jsale.entity.Price;
import org.hutech09.jsale.entity.Product;
import org.hutech09.jsale.web.manager.model.LazyPriceDataModel;

/**
 *
 * @author Nam
 */
@ManagedBean
@SessionScoped
public class PriceBrowseCtrl implements Serializable {

    @EJB
    private IProductManager pm;

    @ManagedProperty("#{priceCuCtrl}")
    private PriceCuCtrl priceCuCtrl;

    private LazyPriceDataModel dataModel;

    private Price selected;

    private Product product;

    @PostConstruct
    public void init() {
        dataModel = new LazyPriceDataModel(pm, null);
    }

    public Price getSelected() {
        return selected;
    }

    public void setSelected(Price selected) {
        this.selected = selected;
    }

    public LazyPriceDataModel getDataModel() {
        return dataModel;
    }

    public String goToPriceCreate() {
        System.err.println(product);
        return priceCuCtrl.showCreate("priceBrowsePage", product);
    }

    public String goToEditPage() {
        return priceCuCtrl.showUpdate(selected, "priceBrowsePage", product);
    }

    public void delete() {
        if (selected != null) {
            try {
                pm.deletePrice(selected.getId());
            } catch (ItemNotFoundException ex) {
                Logger.getLogger(PriceBrowseCtrl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void setDataModel(LazyPriceDataModel dataModel) {
        this.dataModel = dataModel;
    }

    public void setProduct(Product product) {
        this.product = product;
        dataModel = new LazyPriceDataModel(pm, product);
    }

    public PriceCuCtrl getPriceCuCtrl() {
        return priceCuCtrl;
    }

    public void setPriceCuCtrl(PriceCuCtrl priceCuCtrl) {
        this.priceCuCtrl = priceCuCtrl;
    }
}
